<template>
	<sc-water-mark ref="wm" :text="waterMarkContent" :subtext="waterMarkSubtext" :showWater="waterMark=='1'">
		<el-main class="nopadding" v-if="config.DOC_FILE.includes(extName)">
			<vue-office-docx :src="fileUrl" @rendered="rendered"/>
		</el-main>
		<el-main class="nopadding" v-if="config.EXCEL_FILE.includes(extName)">
			<vue-office-excel  :src="fileUrl" @rendered="rendered"/>
		</el-main>
		<el-main class="nopadding" v-if="extName=='.pdf'">
			<div id="mypdf" style="height: 1200px;"></div>
		</el-main>
		<el-main class="nopadding" v-if="extName=='.ofd'">
			<div id='divId' class="ofddiv" style="text-align:-webkit-center;background:gray;padding:30px"></div>
		</el-main>
		<el-main class="nopadding" v-if="config.PPT_FILE.includes(extName)">
			<div id="pptx"></div>
		</el-main>
	</sc-water-mark>
</template>

<script>
import { parseOfdDocument, renderOfd } from "ofd.js";
import PDFObject from 'pdfobject'
import VueOfficeDocx from '@vue-office/docx'
import config from "@/config";
import VueOfficeExcel from "@vue-office/excel";
import tool from "@/utils/tool";
export default {
	computed: {
		config() {
			return config
		}
	},
	components:{
		VueOfficeExcel,
		VueOfficeDocx
	},
	props:{
		fileItem:{type:Object},
	},
	data(){
		return {
			fileUrl:'',
			extName:'.pdf',
			attachId:'',
			netDiskId:'',
			isNetDisk:'',
			waterMarkContent:'',
			waterMarkSubtext:'',
			waterMark:'0',
		}
	},
	watch:{
		fileItem(){
			if(this.fileItem!=null)
			{
				this.showFile(this.fileItem);
			}
		}
	},
	mounted() {
		this.waterMark = this.$TOOL.data.get("WATER_MARK");
		this.waterMarkContent = this.$TOOL.data.get("WATER_MARK_CONTENT");
		this.waterMarkSubtext = this.$TOOL.data.get("WATER_MARK_SUBTEXT");
	},
	methods:{
		rendered(){
			console.log("渲染完成")
		},
		getViewOfd(){
			parseOfdDocument({
				ofd: this.fileUrl,//或者在parseOfdDocument上面先调用接口把返回的值传在这里
				success(res) {
					if (res) {
						let screenWidth = 1000 //容器大小
						const divs = renderOfd(screenWidth, res[0])
						let contentDiv = document.getElementById('divId')
						contentDiv.innerHTML = ''
						for (const div of divs) {
							contentDiv.appendChild(div)
						}

					}
				},
				fail(error) {
					console.log(error, 'errror')
				}
			})
		},
		showFile(item){
			this.extName = item.extName;
			this.netDiskId = item.netDiskId;
			this.attachId = item.attachId;
			this.isNetDisk = item.isNetDisk;
			if(config.PDF_FILE.includes(this.extName))
			{
				if(this.isNetDisk=='true')
				{
					if(this.extName=='.pdf')
					{
						this.fileUrl = config.API_URL+'/get/file/readPdfOnLine?netDiskId=' + tool.crypto.createParam(this.netDiskId) + "&fileName=" + tool.crypto.createParam(this.attachId);
						this.$nextTick(() => {
							PDFObject.embed(this.fileUrl, "#mypdf");
						})
					}else if(this.extName=='.ofd')
					{
						this.fileUrl = config.API_URL+'/get/file/downNetDiskFile?netDiskId=' + tool.crypto.createParam(this.netDiskId) + "&fileName=" + tool.crypto.createParam(this.attachId);
						this.getViewOfd();
					}
				}else {
					if(this.extName=='.pdf')
					{
						this.fileUrl = config.API_URL+'/get/file/readPdfOnLine?attachId=' + tool.crypto.createParam(this.attachId);
						this.$nextTick(() => {
							PDFObject.embed(this.fileUrl, "#mypdf");
						})
					}else if(this.extName=='.ofd')
					{
						this.fileUrl = config.API_URL+'/get/file/getFileDown?attachId=' + tool.crypto.createParam(this.attachId);
						this.getViewOfd();
					}
				}
			}else if(config.PPT_FILE.includes(this.extName)){
				if(this.isNetDisk=='true')
				{
					this.fileUrl = config.API_URL+'/get/file/downNetDiskFile?netDiskId=' + tool.crypto.createParam(this.netDiskId) + "&fileName=" + tool.crypto.createParam(this.attachId);
				}else {
					this.fileUrl = config.API_URL+"/get/file/getFileDown?attachId="+tool.crypto.createParam(this.attachId);
				}
				this.$nextTick(() => {
					$("#pptx").pptxToHtml({
						pptxFileUrl: this.fileUrl, //pptx文件地址
						slidesScale: "100%",
					});
				})
			}else{
				if(this.isNetDisk=='true')
				{
					this.fileUrl = config.API_URL+'/get/file/downNetDiskFile?netDiskId=' + tool.crypto.createParam(this.netDiskId) + "&fileName=" + tool.crypto.createParam(this.attachId);
				}else {
					this.fileUrl = config.API_URL+"/get/file/getFileDown?attachId="+tool.crypto.createParam(this.attachId);
				}
			}
		}
	},
}
</script>
